package com.unidt.www.module.bpm.framework.bpm.core.processor;

import cn.hutool.core.date.DateUtil;
import com.unidt.www.module.bpm.dal.dataobject.definition.ActReActConfigDo;
import com.unidt.www.module.bpm.dal.dataobject.definition.ActRuActTimeDo;
import com.unidt.www.module.bpm.dal.mysql.definition.ActReActConfigMapper;
import com.unidt.www.module.bpm.dal.mysql.definition.ActRuActTimeMapper;
import com.unidt.www.module.bpm.service.definition.BpmProcessDefinitionService;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Model;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @Author sk
 * @Description: 任务节点 截止日期处理器
 * @Date 2023/2/7 14:20
 **/
@Component
public class TaskDeadTimeProcessor {

    @Resource
    ActReActConfigMapper actReActConfigMapper;

    @Resource
    ActRuActTimeMapper actRuActTimeMapper;

    public void doDeadTime(Task task, FlowableEngineEventType eventType) {
        /**
         * 截止日期处理
         */
        String processDefinitionId = task.getProcessDefinitionId();

        String actId = task.getTaskDefinitionKey();
        String taskId = task.getId();
        String taskDefinitionId = task.getTaskDefinitionId();
        String processInstanceId = task.getProcessInstanceId();

        String assignee = task.getAssignee();

        ActRuActTimeDo actRuActTimeDo = this.actRuActTimeMapper.getByTaskId(taskId);

        if (FlowableEngineEventType.TASK_ASSIGNED.equals(eventType)) {
            ActReActConfigDo actReActConfigDo = actReActConfigMapper.getProcessConfig(processDefinitionId, actId);
            if (Objects.nonNull(actReActConfigDo)) {
                /**
                 * 截止日期天数
                 */
                Integer deadDayCount = actReActConfigDo.getDeadDayCount();
                if (deadDayCount != null && deadDayCount != 0 && deadDayCount > 0) {
                    Date deadTime = DateUtil.endOfHour(DateUtil.offsetDay(new Date(), deadDayCount));
                    if (Objects.isNull(actRuActTimeDo)) {

                        actRuActTimeDo = new ActRuActTimeDo(taskDefinitionId, processInstanceId, actId, taskId);
                        actRuActTimeDo.setDeadTime(deadTime);
                        actRuActTimeDo.setAssignee(assignee);
                        this.actRuActTimeMapper.insert(actRuActTimeDo);
                    }
                }

            }
        } else if (FlowableEngineEventType.TASK_COMPLETED.equals(eventType)) {

            if (Objects.nonNull(actRuActTimeDo)) {
                actRuActTimeDo.setEndTime(new Date());
                Date deadTime = actRuActTimeDo.getDeadTime();
                /**
                 * 超时 秒 数
                 */
                Long timeOutSecond = (System.currentTimeMillis() - deadTime.getTime()) / 1000;
                actRuActTimeDo.setTimeOutSecond(timeOutSecond);
                this.actRuActTimeMapper.updateById(actRuActTimeDo);
            }
        } else if (FlowableEngineEventType.ACTIVITY_CANCELLED.equals(eventType)) {
            /**
             * 取消就删除
             */
            if (Objects.nonNull(actRuActTimeDo)) {
                this.actRuActTimeMapper.deleteById(actRuActTimeDo);
            }
        }

    }


}
